package com.wl.emp.service.impl;

import com.wl.emp.dao.EmpDao;
import com.wl.emp.entity.Emp;
import com.wl.emp.service.EmpService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class EmpServiceImpl implements EmpService {

    private SqlSessionFactory factory;

    public EmpServiceImpl(){
        try {
            InputStream is = Resources.getResourceAsStream("sqlSessionFactory.xml");
            factory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int deleteByPrimaryKey(Integer empno) {
        SqlSession session = factory.openSession();
        EmpDao empDao = session.getMapper(EmpDao.class);
        int row = empDao.deleteByPrimaryKey(empno);
        session.commit();
        return row;
    }

    @Override
    public int insert(Emp record) {
        SqlSession session = factory.openSession();
        EmpDao empDao = session.getMapper(EmpDao.class);
        int row= empDao.insert(record);
        session.commit();
        return row;
    }

    @Override
    public int insertSelective(Emp record) {
        SqlSession session = factory.openSession();
        EmpDao empDao = session.getMapper(EmpDao.class);
        int row = empDao.insertSelective(record);
        session.commit();
        return row;
    }

    @Override
    public Emp selectByPrimaryKey(Integer empno) {
        SqlSession session = factory.openSession();
        EmpDao empDao = session.getMapper(EmpDao.class);
        return empDao.selectByPrimaryKey(empno);
    }
    @Override
    public int updateByPrimaryKeySelective(Emp record) {
        SqlSession session = factory.openSession();
        EmpDao empDao = session.getMapper(EmpDao.class);
        int row =  empDao.updateByPrimaryKeySelective(record);
        session.commit();
        return row;
    }
    @Override
    public int updateByPrimaryKey(Emp record) {
        SqlSession session = factory.openSession();
        EmpDao empDao = session.getMapper(EmpDao.class);
        int row = empDao.updateByPrimaryKey(record);
        session.commit();
        return row;
    }
    @Override
    public List<Emp> selectByEname(String ename) {
        SqlSession session = factory.openSession();
        EmpDao empDao = session.getMapper(EmpDao.class);
        return empDao.selectByEname(ename);
    }
    @Override
    public List<Emp> selectByHiredate(Date start, Date end) {
        SqlSession session = factory.openSession();
        EmpDao empDao = session.getMapper(EmpDao.class);
        return empDao.selectByHiredate(start,end);
    }

    @Override
    public List<Emp> selectAll() {
        SqlSession session = factory.openSession();
        EmpDao empDao = session.getMapper(EmpDao.class);
        return empDao.selectAll();
    }
}
